package com.alan.webscoket.chat;

import com.alan.webscoket.vo.ContentVo;
import com.alan.webscoket.vo.Message;
import com.alibaba.fastjson.JSON;

import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.*;

/**
 * Created by Alan on 2017/8/6.
 */
@ServerEndpoint("/chatSocket")
public class ChatSocket {

    private String username;
    private static List<Session> sessions = new ArrayList<>();
    private static List<String> names = new ArrayList<>();
    private static Map<String,Session> map = new HashMap<>();

    public ChatSocket(){
        System.out.println("EchoSocket.EchoSocket()");
    }

    @OnOpen
    public void open(Session session){
        //当前webSocket的Session对象，不是servlet的sesion
        String queryString = session.getQueryString();
        System.out.println(queryString);
        username = queryString.split("=")[1];
        this.names.add(username);
        this.sessions.add(session);
        this.map.put(this.username,session);

        String msg = "欢迎"+this.username+"进入聊天室！！";
        Message message = new Message();
        message.setWelcome(msg);
        message.setUsernames(this.names);
        this.broadcast(this.sessions, JSON.toJSONString(message));
    }

    @OnMessage
    public void message(Session session,String msg){
        ContentVo contentVo = JSON.parseObject(msg,ContentVo.class);
        if(contentVo.getType() == 1){
            //广播
            Message message = new Message();
            message.setContent(this.username,contentVo.getMsg());
            this.broadcast(this.sessions,JSON.toJSONString(message));
        }else {
            //单聊
            // 根据 username 如果找到了，就进行单聊
            String to = contentVo.getTo();
            Session to_session = this.map.get(to);
            if(to_session != null){
                Message message = new Message();
                message.setContent("<font color=red >" + this.username, "私聊："+ contentVo.getMsg() + "</font>");
                try {
                    to_session.getBasicRemote().sendText(JSON.toJSONString(message));
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        //Message message = new Message();
        //message.setContent(this.username,msg);
        // 根据 username 如果找到了，就进行单聊
       // this.broadcast(this.sessions,JSON.toJSONString(message));
    }


    @OnClose
    public void close(Session session){
        this.sessions.remove(session);
        this.names.remove(this.username);
        String msg = "欢送"+this.username + "离开聊天室!!";
        Message message = new Message();
        message.setWelcome(msg);
        message.setUsernames(this.names);
        this.broadcast(this.sessions,JSON.toJSONString(message));
    }

    public void broadcast(List<Session> ss,String msg){
        for (Iterator iterator = ss.iterator(); iterator.hasNext(); ) {
                Session session = (Session) iterator.next();
            try {
                session.getBasicRemote().sendText(msg);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }



}
